﻿using System;
//using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Windows.Forms;

namespace FTPPlugin
{
    class myFTP
    {

        public bool FTPFile(string filename, string newFile)
        {
            try
            {
                Plugin.FTPUserUrl = Plugin.FTPUserUrl.Replace("http://", "");
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + Plugin.FTPUserUrl + newFile);
                request.UsePassive = Plugin.FTPUserPassive;
                request.Method = WebRequestMethods.Ftp.UploadFile;

                request.Credentials = new NetworkCredential(Plugin.FTPUserName, FTPPassword);

                /* Copy the contents of the file to the request stream. */
                StreamReader sourceStream = new StreamReader(filename);
                byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
                sourceStream.Close();
                request.ContentLength = fileContents.Length;
                //                request.Proxy = null; // giving error in Linux so try line below :-)
                request.Proxy = new WebProxy();

                Stream requestStream = request.GetRequestStream();
                requestStream.Write(fileContents, 0, fileContents.Length);
                requestStream.Close();

                FtpWebResponse response = (FtpWebResponse)request.GetResponse();

                response.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error In Upload");
                return false;
            }
            return true;
        }

        /**************************************************************************************************************************/
        /* FTPCreateDir and FTPCheckDir are not used. Created for some other work I was doing.  Leaving for now, still needs work */
        /**************************************************************************************************************************/
        private bool FTPCheckDir(string dirName)
        {
            try
            {
                Plugin.FTPUserUrl = Plugin.FTPUserUrl.Replace("http://", "");
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + Plugin.FTPUserUrl);
                request.UsePassive = Plugin.FTPUserPassive;
                request.UseBinary = true;
                request.Credentials = new NetworkCredential(Plugin.FTPUserName, FTPPassword);

                request.Method = WebRequestMethods.Ftp.ListDirectory;
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();

                StreamReader reader = new StreamReader(response.GetResponseStream());
                string line = reader.ReadLine();
                while (line != null)
                {
                    if (line == dirName)
                    {
                        reader.Close();
                        response.Close();
                        return true;
                    }
                    line = reader.ReadLine();
                }
                reader.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error Creating Directory");
                return false;
            }
            return false;
        }
        private bool FTPCreateDir(string newDir)
        {
            try
            {
                Plugin.FTPUserUrl = Plugin.FTPUserUrl.Replace("http://", "");
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + Plugin.FTPUserUrl + newDir);
                request.UsePassive = Plugin.FTPUserPassive;
                request.UseBinary = true;
                request.Credentials = new NetworkCredential(Plugin.FTPUserName, FTPPassword);
                request.Method = WebRequestMethods.Ftp.MakeDirectory;
                //this fails if directory already exists

                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                Stream requestStream = response.GetResponseStream();
                requestStream.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error Creating Directory");
                return false;
            }
            return true;
        }

        public void setPassword(string newPassword)
        {
            FTPPassword = newPassword;
        }

        private String FTPPassword;
    }
}
